'use strict';

/**
 * @ngdoc function
 * @name afasApp.controller:InventoryCtrl
 * @description
 * # InventoryCtrl
 * Controller of the afasApp
 */
angular.module('afasApp')
  .controller('StocksCtrl', ['$scope', '$state', 'Services', '$filter', '$stateParams', function ($scope, $state, Services, $filter, $stateParams) {

    // $scope.inventories = [];
    $scope.items = [];
    $scope.searchBy = [
       {
        'Value': '',
        'Label': 'By Product Name'
       },
       {
        'Value': '',
        'Label': 'By Code'
       }
    ];
    $scope.categories = [];
    $scope.inventories = [];

    // LOAD PRODUCT CATEGORIES
    Services.getItems('product_categories', {'getall' : true})
    .then(function(results){
      $scope.categories = results.data;
    });

    // LOAD INVENTORIES
    Services.getItems('inventories', {'getall' : true})
    .then(function(results){
      $scope.inventories = results.data;
    });

    $scope.params = {
      'include': 'product,inventory'
    };

    // PAGINATION
    $scope.pagesize = 10;

    $scope.pagination = {
      current: 1
    };

    $scope.pageChanged = function(newPage) {
      getResultsPage(newPage);
    };

    function getResultsPage(pageNumber) {
      var data = $scope.params;
      data.pagesize = $scope.pagesize;
      data.page = (pageNumber - 1);
      $scope.pagination.current = pageNumber;
      Services.getItems('stocks', data)
      .then(function( results ) {
        var pagingData = angular.fromJson(results.headers['x-pagination']);
        $scope.totalItems = pagingData.TotalCount;
        $scope.items = results.data;
      });
    }
    getResultsPage(1);

    $scope.search = function () {
      getResultsPage(1);
    }

  }])
  .controller('StockCreateCtrl', ['$scope', 'Services', '$state', '$stateParams', '$filter', '_', function ($scope, Services, $state, $stateParams, $filter, _) {



  }])
  .controller('StockCreateInCtrl', ['$scope', 'Services', '$state', '$stateParams', '$filter', '_', function ($scope, Services, $state, $stateParams, $filter, _) {

    console.log('Stock In');

    $scope.inventories = [];
    $scope.products = [];
    $scope.stocks = {
      'StockTransactionType' : 6,
      'StockInDetails' : []
    };
    $scope.formAdd = false;
    $scope.formEdit = false;
    $scope.item = {};

    $scope.openItem = function(){
      $scope.formAdd = true;

      Services.getItems('inventories', {'getall' : true})
      .then(function(results){
        $scope.inventories = results.data;
      });

      Services.getItems('products', {'getall' : true})
      .then(function(results){
        $scope.products = results.data;
      });
    }

    $scope.addStock = function(){
      $scope.stocks.StockInDetails.push($scope.item);
      $scope.item = {};
      $scope.formAdd = false;
    }

  }])
  .controller('StockCreateOutCtrl', ['$scope', 'Services', '$state', '$stateParams', '$filter', '_', function ($scope, Services, $state, $stateParams, $filter, _) {

    $scope.add = function () {
      // console.log($scope.newItem);
      Services.add('inventories', $scope.newItem)
      .then(function(results){
        // console.log(results);
        $scope.newItem = {};
        $state.go( 'inventories.list' );
      }, function(results){
        if (results.status == 400) {
          $scope.errors = results.data;
        }
      });
    };

  }])
  .controller('StockCreateTransferCtrl', ['$scope', 'Services', '$state', '$stateParams', '$filter', '_', function ($scope, Services, $state, $stateParams, $filter, _) {

    $scope.add = function () {
      // console.log($scope.newItem);
      Services.add('inventories', $scope.newItem)
      .then(function(results){
        // console.log(results);
        $scope.newItem = {};
        $state.go( 'inventories.list' );
      }, function(results){
        if (results.status == 400) {
          $scope.errors = results.data;
        }
      });
    };

  }])
  .controller('StockCtrl', ['$scope', 'Services', '$state', '$stateParams', function ($scope, Services, $state, $stateParams) {

    $scope.current = {};
    $scope.currentID = $stateParams.id;
    $scope.current = Services.getItem('inventories', $stateParams.id);

    $scope.update = function () {
      Services.update('inventories', $scope.current, $scope.currentID);
      $state.go( 'inventories.show', {id: $scope.currentID} );
    };
  }]);
